package 字符串;

import java.util.HashMap;
import java.util.Map;

/**
 * @PackageName: 字符串
 * @ClassName: LeetCode2284
 * @Author: chen jinxu
 * @Date: 2023/3/12 16:39
 * @Description: //最多单词数的发件人
 * 给你一个聊天记录，共包含 n条信息。给你两个字符串数组messages 和senders，其中messages[i]是senders[i]发出的一条信息。
 * 一条 信息是若干用单个空格连接的 单词，信息开头和结尾不会有多余空格。发件人的 单词计数是这个发件人总共发出的 单词数。注意，
 * 一个发件人可能会发出多于一条信息。
 * 请你返回发出单词数 最多的发件人名字。如果有多个发件人发出最多单词数，请你返回 字典序最大的名字。
 * 注意：
 * 字典序里，大写字母小于小写字母。
 * "Alice" 和"alice"是不同的名字。
 * 输入：messages = ["Hello userTwooo","Hi userThree","Wonderful day Alice","Nice day userThree"],
 * senders = ["Alice","userTwo","userThree","Alice"]
 * 输出："Alice"
 * 解释：Alice 总共发出了 2 + 3 = 5 个单词。
 * userTwo 发出了 2 个单词。
 * userThree 发出了 3 个单词。
 * 由于 Alice 发出单词数最多，所以我们返回 "Alice" 。
 * 输入：messages = ["How is leetcode for everyone","Leetcode is useful for practice"], senders = ["Bob","Charlie"]
 * 输出："Charlie"
 * 解释：Bob 总共发出了 5 个单词。
 * Charlie 总共发出了 5 个单词。
 * 由于最多单词数打平，返回字典序最大的名字，也就是 Charlie 。
 * ["tP x M VC h lmD","D X XF w V","sh m Pgl","pN pa","C SL m G Pn v","K z UL B W ee","Yf yo n V U Za f np","j J sk f qr e v t","L Q cJ c J Z jp E","Be a aO","nI c Gb k Y C QS N","Yi Bts","gp No g s VR","py A S sNf","ZS H Bi De dj dsh","ep MA KI Q Ou"]
 * ["OXlq","IFGaW","XQPeWJRszU","Gb","HArIr","Gb","FnZd","FnZd","HArIr","OXlq","IFGaW","XQPeWJRszU","EMoUs","Gb","EMoUs","EMoUs"]
 */
public class LeetCode2284 {
    public static void main(String[] args) {
        System.out.println(largestWordCount(
                new String[]{"Hello userTwooo", "Hi userThree", "Wonderful day Alice", "Nice day userThree"},
                new String[]{"Alice", "userTwo", "userThree", "Alice"}));
        System.out.println(largestWordCount(
                new String[]{"How is leetcode for everyone", "Leetcode is useful for practice"},
                new String[]{"Bob", "Charlie"}));
        System.out.println(largestWordCount(
                new String[]{"tP x M VC h lmD","D X XF w V","sh m Pgl","pN pa","C SL m G Pn v","K z UL B W ee","Yf yo n V U Za f np","j J sk f qr e v t","L Q cJ c J Z jp E","Be a aO","nI c Gb k Y C QS N","Yi Bts","gp No g s VR","py A S sNf","ZS H Bi De dj dsh","ep MA KI Q Ou"},
                new String[]{"OXlq","IFGaW","XQPeWJRszU","Gb","HArIr","Gb","FnZd","FnZd","HArIr","OXlq","IFGaW","XQPeWJRszU","EMoUs","Gb","EMoUs","EMoUs"}));
    }

    public static String largestWordCount(String[] messages, String[] senders) {
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < senders.length; i++) {
            String[] words = messages[i].split("\\s");
            map.put(senders[i], map.getOrDefault(senders[i], 0) + words.length);
        }
        int maxNum = 0;
        String result = "";
        for (String name : map.keySet()) {
            if (map.get(name) > maxNum) {
                maxNum = map.get(name);
                result = name;
            } else if (map.get(name) == maxNum) {
                if (name.compareTo(result) > 0) {
                    result = name;
                }
            }
        }
        return result;
    }
}
